home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / TELECOM / PARCP100 / DOC / LOWLEVEL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-21  |  4.3 KB  |  174 lines

  1. /*
  2.  * The following code has been used in PARCP v0.70b. I have included its
  3.  * source for those of you who would like to use my UNI-BI adapter in your own
  4.  * applications.
  5.  */
  6.  
  7. /*
  8.  * PARallel CoPy - written for transferring large files between any two
  9.  * machines with parallel ports
  10.  *
  11.  * (c) Petr Stehlik, 1996
  12.  *
  13.  * You will need a special UNI-BI HW adapter if your PC offers unidirectional
  14.  * parallel ports only.
  15.  */
  16.  
  17. typedef unsigned char    BYTE;
  18. typedef unsigned int     WORD;
  19. typedef enum {FALSE,TRUE} BOOLEAN;
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <stdlib.h>
  25.  
  26. #ifdef __MSDOS__
  27. #include <dos.h>
  28. #include <conio.h>
  29. #include <time.h>
  30. #include <sys\stat.h>
  31.  
  32. int print_port;
  33. int gcontrol = (1 << 2);    /* Ucc pro HW interface */
  34.  
  35. #define SET_CTRL(x)    outportb(print_port+2, gcontrol = (gcontrol | (1 << x)))
  36. #define CLR_CTRL(x)    outportb(print_port+2, gcontrol = (gcontrol &~(1 << x)))
  37. #define STATUS    inportb(print_port+1)
  38.  
  39. /* Po resetu PC: STROBE, AUTOLF, INIT jsou +5V, zatimco SLCTIN je 0V) */
  40. /* STROBE je 0. bit a je negovany - log.1 = 0V, log.0 = +5V */
  41. /* AUTOLF je 1. bit a je negovany - log.1 = 0V, log.0 = +5V */
  42. /* SLCTIN je 3. bit a je negovany - log.1 = 0V, log.0 = +5V */
  43. /* INIT je 2. bit, log.1 = +5V, coz napaji HW interface */
  44.  
  45. #define STROBE_HIGH    CLR_CTRL(0)
  46. #define STROBE_LOW    SET_CTRL(0)
  47. #define IS_READY    !(STATUS & 0x80)
  48.  
  49. #define CLOCK_LOW    SET_CTRL(3)
  50. #define CLOCK_HIGH    CLR_CTRL(3)
  51. #define CLOCK_RAISE    {CLOCK_LOW; CLOCK_HIGH;}    /* impuls pro latch k presypani dat na vystup */
  52. #define GET_LOW_NIBBLE    ((STATUS >> 3) & 0x0f)
  53. #define GET_HIGH_NIBBLE    ((STATUS << 1) & 0xf0)
  54.  
  55. #define SET_OUTPUT    SET_CTRL(1)
  56. #define SET_INPUT    CLR_CTRL(1)
  57. #define GET_BYTE(x)                \
  58.     {                            \
  59.         CLOCK_LOW;                \
  60.         x=GET_LOW_NIBBLE;        \
  61.         CLOCK_HIGH;                \
  62.         x|=GET_HIGH_NIBBLE;        \
  63.     }
  64.  
  65. #define PUT_BYTE(x)                \
  66.     {                            \
  67.         outportb(print_port,x);    \
  68.         CLOCK_RAISE;             \
  69.     }
  70.  
  71. #define GODMODE        /* ma smysl jen pro TOS */
  72. #define USERMODE    /* ma smysl jen pro TOS */
  73.  
  74. #define KEYPRESSED    kbhit()
  75.  
  76. #endif    /* __MSDOS__ */
  77.  
  78. /*******************************************************************************/
  79.  
  80. #ifdef __TOS__
  81. #include <tos.h>
  82. #include <ext.h>
  83.  
  84. BYTE    *DATAREAD=(BYTE *)0xffff8800L,
  85.         *REGSEL=(BYTE *)0xffff8800L,
  86.         *DATAWRITE=(BYTE *)0xffff8802L,
  87.         *GPIP=(BYTE *)0xfffffa01L;
  88.  
  89. int cli(void)    0x40c0;    /* move.w sr,d0 */
  90. void sti(int)    0x46c0; /* move.w d0,sr */
  91.  
  92. int status_register;
  93. #define CLI        sti((status_register=cli()) | 0x700)
  94. #define STI        sti(status_register)
  95.  
  96. #define SET_CTRL(x,y)    {CLI; *REGSEL=x; *DATAWRITE=*DATAREAD | (1 << y); STI;}
  97. #define CLR_CTRL(x,y)    {CLI; *REGSEL=x; *DATAWRITE=*DATAREAD &~(1 << y); STI;}
  98.  
  99. #define STROBE_HIGH    SET_CTRL(14,5)
  100. #define STROBE_LOW    CLR_CTRL(14,5)
  101. #define SET_OUTPUT    SET_CTRL(7,7)
  102. #define SET_INPUT    CLR_CTRL(7,7)
  103.  
  104. #define IS_READY    (*GPIP & 1)
  105. #define GET_BYTE(x)    {CLI; *REGSEL=15; x=*DATAREAD; STI;}
  106. #define PUT_BYTE(x)    {CLI; *REGSEL=15; *DATAWRITE=x; STI;}
  107.  
  108. static BOOLEAN SuSuper=FALSE;
  109. static void *stack;
  110. #define    GODMODE        { if (! SuSuper) {stack=(void *)Super(0L); SuSuper = TRUE; } }
  111. #define    USERMODE    { if (SuSuper) { Super(stack); SuSuper = FALSE; } }
  112.  
  113. #define KEYPRESSED    (Kbshift(-1)&3)
  114.  
  115. #endif    /* __TOS__ */
  116.  
  117. /*******************************************************************************/
  118. #define EXIT(a)    { USERMODE; errexit(a); }
  119.  
  120. #define TIMEOUT_CYCLE    1E6
  121. #define WAIT_LOW        {long i=TIMEOUT_CYCLE; while(IS_READY && i>0) i--; if (i == 0) EXIT("Timeout.");}
  122. #define WAIT_HIGH        {long i=TIMEOUT_CYCLE; while(! IS_READY && i>0) i--; if (i == 0) EXIT("Timeout.");}
  123. #define WAIT_LOW_KEY    {while(IS_READY) if (KEYPRESSED) EXIT("Stopped.");}
  124. #define WAIT_HIGH_KEY    {while(! IS_READY) if (KEYPRESSED) EXIT("Stopped.");}
  125.  
  126. void read_block(BYTE *block, long n)
  127. {
  128.     BYTE x;
  129.     long i;
  130.  
  131.     GODMODE;
  132.  
  133.     SET_INPUT;
  134.     WAIT_LOW_KEY;
  135.  
  136.     STROBE_LOW;WAIT_HIGH;
  137.  
  138.     for(i=0; i<n; i++) {
  139.         GET_BYTE(x);
  140.         block[i++]=x;
  141.         STROBE_HIGH;WAIT_LOW;
  142.  
  143.         GET_BYTE(x);
  144.         block[i]=x;
  145.         STROBE_LOW;WAIT_HIGH;
  146.     }
  147.     STROBE_HIGH;WAIT_HIGH;
  148.  
  149.     USERMODE;
  150. }
  151.  
  152. void write_block(BYTE *block, long n)
  153. {
  154.     long i;
  155.     BYTE x;
  156.  
  157.     GODMODE;
  158.  
  159.     STROBE_LOW;WAIT_LOW_KEY;
  160.  
  161.     SET_OUTPUT;
  162.     for(i=0; i<n; i++) {
  163.         PUT_BYTE(x=block[i++]);
  164.         STROBE_HIGH;WAIT_HIGH;
  165.  
  166.         PUT_BYTE(x=block[i]);
  167.         STROBE_LOW;WAIT_LOW;
  168.     }
  169.     SET_INPUT;
  170.     STROBE_HIGH;WAIT_HIGH;
  171.  
  172.     USERMODE;
  173. }
  174.